home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / OPMQV1 (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  3.8 KB  |  173 lines

  1. package com.sun.java.swing;
  2.  
  3. class TimerQueue implements Runnable {
  4.    Timer firstTimer;
  5.    boolean running;
  6.    private static final Object sharedInstanceKey = new StringBuffer("TimerQueue.sharedInstanceKey");
  7.    private static final Object expiredTimersKey = new StringBuffer("TimerQueue.expiredTimersKey");
  8.    static Class class$com$sun$java$swing$TimerQueue;
  9.  
  10.    public TimerQueue() {
  11.       this.start();
  12.    }
  13.  
  14.    synchronized void addTimer(Timer timer, long expirationTime) {
  15.       if (!timer.running) {
  16.          Timer previousTimer = null;
  17.  
  18.          Timer nextTimer;
  19.          for(nextTimer = this.firstTimer; nextTimer != null && nextTimer.expirationTime <= expirationTime; nextTimer = nextTimer.nextTimer) {
  20.             previousTimer = nextTimer;
  21.          }
  22.  
  23.          if (previousTimer == null) {
  24.             this.firstTimer = timer;
  25.          } else {
  26.             previousTimer.nextTimer = timer;
  27.          }
  28.  
  29.          timer.expirationTime = expirationTime;
  30.          timer.nextTimer = nextTimer;
  31.          timer.running = true;
  32.          this.notify();
  33.       }
  34.    }
  35.  
  36.    synchronized boolean containsTimer(Timer timer) {
  37.       return timer.running;
  38.    }
  39.  
  40.    synchronized long postExpiredTimers() {
  41.       long timeToWait;
  42.       do {
  43.          Timer timer = this.firstTimer;
  44.          if (timer == null) {
  45.             return 0L;
  46.          }
  47.  
  48.          long currentTime = System.currentTimeMillis();
  49.          timeToWait = timer.expirationTime - currentTime;
  50.          if (timeToWait <= 0L) {
  51.             try {
  52.                timer.post();
  53.             } catch (SecurityException var7) {
  54.             }
  55.  
  56.             this.removeTimer(timer);
  57.             if (timer.isRepeats()) {
  58.                this.addTimer(timer, currentTime + (long)timer.getDelay());
  59.             }
  60.          }
  61.  
  62.          try {
  63.             this.wait(1L);
  64.          } catch (InterruptedException var6) {
  65.          }
  66.       } while(timeToWait <= 0L);
  67.  
  68.       return timeToWait;
  69.    }
  70.  
  71.    synchronized void removeTimer(Timer timer) {
  72.       if (timer.running) {
  73.          Timer previousTimer = null;
  74.          Timer nextTimer = this.firstTimer;
  75.  
  76.          boolean found;
  77.          for(found = false; nextTimer != null; nextTimer = nextTimer.nextTimer) {
  78.             if (nextTimer == timer) {
  79.                found = true;
  80.                break;
  81.             }
  82.  
  83.             previousTimer = nextTimer;
  84.          }
  85.  
  86.          if (found) {
  87.             if (previousTimer == null) {
  88.                this.firstTimer = timer.nextTimer;
  89.             } else {
  90.                previousTimer.nextTimer = timer.nextTimer;
  91.             }
  92.  
  93.             timer.expirationTime = 0L;
  94.             timer.nextTimer = null;
  95.             timer.running = false;
  96.          }
  97.       }
  98.    }
  99.  
  100.    public synchronized void run() {
  101.       while(this.running) {
  102.          long timeToWait = this.postExpiredTimers();
  103.  
  104.          try {
  105.             this.wait(timeToWait);
  106.          } catch (InterruptedException var3) {
  107.          }
  108.       }
  109.  
  110.    }
  111.  
  112.    public static TimerQueue sharedInstance() {
  113.       Class var10000 = class$com$sun$java$swing$TimerQueue;
  114.       if (var10000 == null) {
  115.          try {
  116.             var10000 = Class.forName("com.sun.java.swing.TimerQueue");
  117.          } catch (ClassNotFoundException var3) {
  118.             throw new NoClassDefFoundError(((Throwable)var3).getMessage());
  119.          }
  120.  
  121.          class$com$sun$java$swing$TimerQueue = var10000;
  122.       }
  123.  
  124.       synchronized(var10000) {
  125.          TimerQueue sharedInst = (TimerQueue)SwingUtilities.appContextGet(sharedInstanceKey);
  126.          if (sharedInst == null) {
  127.             sharedInst = new TimerQueue();
  128.             SwingUtilities.appContextPut(sharedInstanceKey, sharedInst);
  129.          }
  130.  
  131.          return sharedInst;
  132.       }
  133.    }
  134.  
  135.    synchronized void start() {
  136.       if (this.running) {
  137.          throw new RuntimeException("Can't start a TimerQueue that is already running");
  138.       } else {
  139.          Thread timerThread = new Thread(this, "TimerQueue");
  140.  
  141.          try {
  142.             timerThread.setDaemon(true);
  143.          } catch (SecurityException var2) {
  144.          }
  145.  
  146.          timerThread.start();
  147.          this.running = true;
  148.       }
  149.    }
  150.  
  151.    synchronized void stop() {
  152.       this.running = false;
  153.       this.notify();
  154.    }
  155.  
  156.    public synchronized String toString() {
  157.       StringBuffer buf = new StringBuffer();
  158.       buf.append("TimerQueue (");
  159.       Timer nextTimer = this.firstTimer;
  160.  
  161.       while(nextTimer != null) {
  162.          buf.append(nextTimer.toString());
  163.          nextTimer = nextTimer.nextTimer;
  164.          if (nextTimer != null) {
  165.             buf.append(", ");
  166.          }
  167.       }
  168.  
  169.       buf.append(")");
  170.       return buf.toString();
  171.    }
  172. }
  173.